package xyz.xwls.bootadmin.util;

import xyz.xwls.bootadmin.sys.domain.Auth;
import xyz.xwls.bootadmin.sys.domain.Role;
import xyz.xwls.bootadmin.sys.domain.RoleAuth;
import xyz.xwls.bootadmin.sys.repository.AuthRepository;
import xyz.xwls.bootadmin.sys.repository.RoleAuthRepository;
import xyz.xwls.bootadmin.sys.repository.RoleRepository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author WZW
 */
public class InitUtil {

    private List<Auth> initAuth(AuthRepository authRepository) {
        List<Auth> authList = new ArrayList<>();
        Auth auth1 = Auth.builder().parentId(0L).name("系统管理").value("sys:menu").type(0).status(1).createTime(new Date()).build();
        authRepository.save(auth1);
        Auth auth2 = Auth.builder().parentId(auth1.getId()).name("用户管理").value("sys:user:user").type(1).status(1).createTime(new Date()).url("/sys/user").build();
        authRepository.save(auth2);
        Auth auth21 = Auth.builder().parentId(auth2.getId()).name("新增").value("sys:user:add").type(2).status(1).createTime(new Date()).build();
        Auth auth22 = Auth.builder().parentId(auth2.getId()).name("编辑").value("sys:user:edit").type(2).status(1).createTime(new Date()).build();
        Auth auth23 = Auth.builder().parentId(auth2.getId()).name("删除").value("sys:user:remove").type(2).status(1).createTime(new Date()).build();
        authRepository.save(auth21);
        authRepository.save(auth22);
        authRepository.save(auth23);
        Auth auth3 = Auth.builder().parentId(auth1.getId()).name("角色管理").value("sys:role:role").type(1).status(1).createTime(new Date()).url("/sys/role").build();
        authRepository.save(auth3);
        Auth auth31 = Auth.builder().parentId(auth3.getId()).name("新增").value("sys:role:add").type(2).status(1).createTime(new Date()).build();
        Auth auth32 = Auth.builder().parentId(auth3.getId()).name("编辑").value("sys:role:edit").type(2).status(1).createTime(new Date()).build();
        Auth auth33 = Auth.builder().parentId(auth3.getId()).name("删除").value("sys:role:remove").type(2).status(1).createTime(new Date()).build();
        authRepository.save(auth31);
        authRepository.save(auth32);
        authRepository.save(auth33);
        Auth auth4 = Auth.builder().parentId(auth1.getId()).name("权限管理").value("sys:auth:auth").type(1).status(1).createTime(new Date()).url("/sys/auth").build();
        authRepository.save(auth4);
        Auth auth41 = Auth.builder().parentId(auth4.getId()).name("新增").value("sys:auth:add").type(2).status(1).createTime(new Date()).build();
        Auth auth42 = Auth.builder().parentId(auth4.getId()).name("编辑").value("sys:auth:edit").type(2).status(1).createTime(new Date()).build();
        Auth auth43 = Auth.builder().parentId(auth4.getId()).name("删除").value("sys:auth:remove").type(2).status(1).createTime(new Date()).build();
        authRepository.save(auth41);
        authRepository.save(auth42);
        authRepository.save(auth43);
        authList.add(auth1);
        authList.add(auth2);
        authList.add(auth21);
        authList.add(auth22);
        authList.add(auth23);
        authList.add(auth3);
        authList.add(auth31);
        authList.add(auth32);
        authList.add(auth33);
        authList.add(auth4);
        authList.add(auth41);
        authList.add(auth42);
        authList.add(auth43);
        return authList;
    }


    private Role initRole(RoleRepository roleRepository) {
        Role role = Role.builder().name("admin").description("超级管理员").status(1).createTime(new Date()).build();
        return roleRepository.save(role);
    }

    public void initRoleAuth(RoleAuthRepository roleAuthRepository, RoleRepository roleRepository, AuthRepository authRepository) {
        List<Auth> authList = initAuth(authRepository);
        Role role = initRole(roleRepository);
        List<RoleAuth> roleAuthList = new ArrayList<>();
        authList.forEach(auth -> {
            RoleAuth roleAuth = RoleAuth.builder().roleId(role.getId()).authId(auth.getId()).build();
            roleAuthList.add(roleAuth);
        });
        roleAuthRepository.saveAll(roleAuthList);
    }

    public void initUser(){


    }

}
